<template>
  <div class="queryBox">
    <div class="left">
      <el-icon>
        <Tickets />
      </el-icon>
      <span> 数据列表 </span>
    </div>
    <div class="right">
      <el-button @click="createList">添加</el-button>
    </div>
  </div>
  <div>
    <el-table :data="list" style="width: 100%" border>
      <el-table-column prop="id" label="编号" />
      <el-table-column prop="name" label="类型名称" />
      <el-table-column prop="attributeCount" label="属性数量" />
      <el-table-column prop="paramCount" label="参数数量" />
      <el-table-column prop="email" label="设置">
        <template #default="{ row }">
          <div class="buttonBox">
            <el-button @click="attributeJump(row.id, 0)" :disabled="params.parentId !== 0">属性列表</el-button>
            <el-button @click="attributeJump(row.id, 1)" :disabled="params.parentId !== 0">参数列表</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="操作">
        <template #default="{ row }">
          <el-button @click="updateList(row.id)">编辑</el-button>
          <el-button type="danger" @click="deleteList(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <ElConfigProvider :locale="zhCn">
      <el-pagination v-model:current-page="params.pageNum" v-model:page-size="params.pageSize"
        :page-sizes="[10, 20, 50, 100]" background layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="productList" @current-change="productList" />
    </ElConfigProvider>
  </div>
</template>
<script setup lang="ts">
import { Message, Tickets, Warning } from "@element-plus/icons-vue";
import {
  ElIcon,
  ElButton,
  ElTable,
  ElTableColumn,
  ElMessage,
  ElMessageBox,
  ElConfigProvider,
  ElPagination,
} from "element-plus";
import { reactive, ref } from "vue";
import {
  getProductAttributesListApi,
  updateProductAPI,
  deleteProductAPI,
  createProductAPI,
} from "./api/index";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { useRouter } from "vue-router";
import productAttrList from "./productAttrList.vue"

const router = useRouter();

const params = reactive({
  pageNum: 1,
  pageSize: 5,
  parentId: 0,
});
const total = ref(0);

const list = ref<IProductLList[]>([]);

// 页面数据请求
const productList = () => {
  getProductAttributesListApi(params).then((res) => {
    if (res.code === 200) {
      list.value = res.data.list;
      total.value = res.data.total;
    }
  });
};

// 编辑
const updateList = (id: number) => {
  ElMessageBox.prompt("类型名称", "编辑类型", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(({ value }) => {
      updateProductAPI(id, value).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: "success",
            message: "修改成功",
          });
        } else {
          ElMessage({
            type: "warning",
            message: "修改失败请联系管理员",
          });
        }
      });
      productList();
    })
    .catch(() => { });
};

// 删除
const deleteList = (id: number) => {
  ElMessageBox.confirm("是否删除该品牌", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteProductAPI(id).then((res) => {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
      });
      productList();
    })
    .catch(() => { });
};

// 添加
const createList = () => {
  ElMessageBox.prompt("类型名称", "添加类型", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(({ value }) => {
      const name = value;
      console.log(value);
      createProductAPI(name).then((res) => {
        if (res.code === 200) {
          ElMessage({
            type: "success",
            message: "添加成功",
          });
        } else {
          ElMessage({
            type: "warning",
            message: "修改失败请联系管理员",
          });
        }
      });
      productList();
    })
    .catch(() => { });
};

// 跳转到属性列表和分类列表
const attributeJump = (cid: number, type: number) => {
  router.push(
    `/pms/productAttrList?cid=${cid}&pageNum=${params.pageNum}&pageSize=${params.pageSize}&type=${type}`
  );
};


productList();
</script>
<style lang="less" scoped>
.queryBox {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 5px;
  padding: 10px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;

  .left {
    font-size: 15px;
  }
}

:deep(.cell) {
  display: flex;
  // justify-content: space-between;
}

:deep(.el-button) {
  font-size: 12px;
  padding: 7px;
}
.page{
  float: right;
}
</style>
